package com.sysmanage.etl.controls;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.sysmanage.etl.model.ETL_Property;
import com.sysmanage.etl.model.ETL_Rule;
import com.sysmanage.etl.model.ETL_Table;
import com.sysmanage.etl.service.IRuleService;
import com.sysmanage.etl.service.ITableService;
import com.sysmanage.etl.utils.PropertyUtils;
import com.sysmanage.etl.utils.StringUtils;
import com.sysmanage.etl.vo.TableVO;

@Scope("prototype")
@Controller(value = "tableTg")
public class TableTg extends BaseETLTg implements ModelDriven<TableVO> {
	private static final long serialVersionUID = 8132806575643085910L;

	@Resource
	private ITableService tableService;
	@Resource
	private IRuleService ruleService;

	private Integer dataSourceId;

	private Integer tableId;

	private TableVO tableVO;

	private String key;

	private List<TableVO> datas = new ArrayList<TableVO>(0);
	private Map<String, Object> result = new HashMap<String, Object>();

	public String data() {
		return "dataJSON";
	}

	public void prepareData() {
		if (StringUtils.isNotBlank(this.key)) {

		}
		if (this.key != null && !this.key.isEmpty()) {
			List<ETL_Table> tables = this.tableService.findList(
					"from ETL_Table where dataSource.dataSource_id = "
							+ dataSourceId + " and table_name like '%" + key
							+ "%'", page, rows);
			for (ETL_Table table : tables) {
				TableVO vo = new TableVO();
				BeanUtils.copyProperties(table, vo);
				this.datas.add(vo);
			}
			this.total = this.tableService
					.findCountBySql("from ETL_Table where dataSource.dataSource_id = "
							+ dataSourceId
							+ " and table_name like '%"
							+ key
							+ "%'");
		} else {
			List<ETL_Table> tables = this.tableService.findList(
					"from ETL_Table where dataSource.dataSource_id = "
							+ dataSourceId + " and display = " + true, page,
					rows);
			for (ETL_Table table : tables) {
				TableVO vo = new TableVO();
				BeanUtils.copyProperties(table, vo);
				this.datas.add(vo);
			}
			this.total = this.tableService
					.findCountBySql("from ETL_Table where dataSource.dataSource_id = "
							+ dataSourceId + " and display = " + true);
		}
	}

	/**
	 * 
	 * 重新加载数据源表中字段
	 * 
	 * TODO 重新加载数据源表中字段 ---方法已写好，还未添加到页面
	 */
	public void reloadProperty() {
		ETL_Table table = tableService.findById(tableId);
		if (table != null) {
			List<ETL_Property> properties = PropertyUtils.change2ETL_Property(table.getDataSource(), table.getTable_name());
			tableService.reloadProperty(table, properties);
		}
		result.put("success", true);
		result.put("message", "重新加载数据源成功");
		JSONObject json = JSONObject.fromObject(result);
		this.outJsonPlainString(response, json.toString());
	}

	public String edit() {
		return "updateJSON";
	}

	public void prepareEdit() {
		ETL_Table table = this.tableService.findById(this.tableVO.getTable_id());
		boolean isUpdateSuccess;
		if (!table.getDisplay()) {
			isUpdateSuccess = this.tableService.alterTable(table);
		} else {
			List<ETL_Rule> rules = ruleService.findList("from ETL_Rule r where r.table.id = "+ table.getTable_id());
			if (rules != null && rules.size() > 0) {
				result.put("message", "对不起，此表已配置导入规则，请先删除规则后再移除！");
				result.put("success", true);
				return;
			}
			isUpdateSuccess = this.tableService.deleteTable(table);
		}

		if (isUpdateSuccess) {
			result.put("message", "操作成功!");
			result.put("success", true);
		} else {
			result.put("message", "操作失败!");
			result.put("success", true);
		}
	}

	public String update() {
		return "updateJSON";
	}

	public void prepareUpdate() {
		ETL_Table table = this.tableService
				.findById(this.tableVO.getTable_id());
		table.setTable_alais(this.tableVO.getTable_alais());
		boolean isUpdateSuccess = this.tableService.alter(table);
		if (isUpdateSuccess) {
			result.put("message", "操作成功!");
			result.put("success", true);
		} else {
			result.put("message", "操作失败!");
			result.put("success", false);
		}
	}

	public String delete() {
		return "updateJSON";
	}

	public void prepareDelete() {
		boolean isDeleteSuccess = this.tableService.deleteTable(this.tableVO
				.getTable_id());
		if (isDeleteSuccess) {
			result.put("message", "操作成功!");
			result.put("success", true);
		} else {
			result.put("message", "操作失败!");
			result.put("success", false);
		}
	}

	@JSON(name = "rows")
	public List<TableVO> getDatas() {
		return datas;
	}

	public void setDataSourceId(Integer dataSourceId) {
		this.dataSourceId = dataSourceId;
	}

	public void setTableVO(TableVO tableVO) {
		this.tableVO = tableVO;
	}

	public void setKey(String key) {
		this.key = key;
	}

	@Override
	public TableVO getModel() {
		if (tableVO == null) {
			tableVO = new TableVO();
		}
		return tableVO;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	private Integer total = 0;

	public Integer getTotal() {
		return this.total;
	}

	public void setTableId(Integer tableId) {
		this.tableId = tableId;
	}

}
